<template>
  <div class="add-box">
    <Card>
      <span slot="title">添加账号</span>
      <div slot="content">
        <el-form :model="accAddData" :rules="rules" ref="accAdd" label-width="100px" class="demo-ruleForm" status-icon hide-required-asterisk>
          <el-form-item label="账号" prop="name">
            <el-input v-model="accAddData.name" type="text"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input v-model="accAddData.pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="renewPwd">
            <el-input type="password" v-model="accAddData.renewPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户组" prop="group">
            <el-select v-model="accAddData.group" placeholder="请选择用户组">
              <el-option label="超级管理员" value="超级管理员"></el-option>
              <el-option label="普通管理员" value="普通管理员"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm">立即创建</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </Card>

  </div>
</template>

<script>
import Card from "@/components/Card.vue";
import { NAM_REG, PWD_REG } from "@/utils/reg.js";
import { addAcc_api } from "@/api/user.js";

export default {
  components: {
    Card,
  },
  data() {
    //自定义表单验证
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号名不能为空"));
      } else if (!NAM_REG.test(value)) {
        return callback(new Error("账号名必须为3-8位"));
      } else {
        return callback();
      }
    };
    var checkPwd = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      } else if (!PWD_REG.test(value)) {
        return callback(new Error("密码必须为3-8位"));
      } else {
        return callback();
      }
    };
    var checkRenewPwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.accAddData.pwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      accAddData: {
        name: "",
        pwd: "",
        renewPwd: "",
        group: "",
      },

      rules: {
        name: [{ validator: checkName, trigger: "blur" }],
        pwd: [{ validator: checkPwd, trigger: "blur" }],
        renewPwd: [{ validator: checkRenewPwd, trigger: "blur" }],
        group: [{ required: true, message: "请选择用户组", trigger: "change" }],
      },
    };
  },
  methods: {
    //提交
    submitForm() {
      console.log(this.accAddData);
      this.$refs.accAdd.validate(async (valid) => {
        if (valid) {
          await addAcc_api({
            account: this.accAddData.name,
            password: this.accAddData.pwd,
            userGroup: this.accAddData.group,
          });
          this.$router.push("/account/accountList");
        } else {
          return false;
        }
      });
    },
    //重置
    resetForm() {
      this.$refs.accAdd.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.add-box {
  /deep/.el-input {
    width: 300px;
  }
}
</style>